﻿using System;
using System.Globalization;

#if WINDOWS_UWP || NETFX_CORE
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Imaging;
#else
using System.Windows.Data;
using System.Windows.Media.Imaging;
#endif

using Outcoder.Data.ImageCache;

namespace Outcoder.UI.Xaml.Converters
{
	/// <summary>
	/// Provides image cache support for Image Source bindings.
	/// </summary>
	public class ImageCacheConverter : IValueConverter
	{
		public object Convert(object value, Type targetType,
			object parameter, CultureInfo culture)
		{
			string url = value as string;
			if (url != null)
			{
				return ImageCache.GetImage(new BitmapImage(new Uri(url)));
			}

			BitmapImage bitmapImage = value as BitmapImage;
			if (bitmapImage != null)
			{
				return ImageCache.GetImage(bitmapImage);
			}
			return value;
		}

		public object ConvertBack(object value, Type targetType,
			object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
